﻿using System.Collections.Generic;

namespace Manoso.Data.FluentMap
{
    public interface IMapDeclare<out T> : IMapSql<T>
    {
    }

    internal class MapDeclare<T> : MapSql<T>, IMapDeclare<T>, IMapCommand
    {
        public MapDeclare()
        {
            IsMatch = NOT_MATCH;
        }

        public virtual void GetCommand(IMapTask task, IList<string> stack)
        {
            var sub = (T)task.Values[TableAlias];
            if (!IsMatch(sub)) return;

            GetReference(task, stack);
        }

        protected void GetReference(IMapTask task, IList<string> stack)
        {
            var query = new List<string>();
            var id = GetDeclare(task, stack);
            if (id == null) return;

            var idColumn = id.Column;
            var assignment = task.Adaptor.GetAssignment(idColumn);

            SetAssignment(task, stack, id, assignment);
            query.Add(assignment);
            task.AddCommand(string.Join(string.Empty, query), MapCommandType.Assign);
        }
    }
}
